/*
 * MainServer.java
 *
 * Created on 1 de Outubro de 2007, 20:50
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Remote;

import DB.DataAccess;
import Util.Observer;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author Ronaldinho
 */
public class MainServer extends Connectable implements Observer {
    
    private int minPort = 1032;
    private int maxPort = 62000;
    
    private Vector<Integer> usedPorts;
    private DataAccess dataAccess;
    
    /**
     * Creates a new instance of MainServer
     */
    public MainServer() throws IOException {
        super(10000, 10001);
        this.usedPorts = new Vector<Integer>();
        this.dataAccess = new DataAccess();
    }
    
    protected void conectar() {
        Integer i;
        Vector<Object> info = new Vector<Object>();
        Message message;
        try {
            this.receberMensagem();
            
            /**C�digo para validar
             *usuario*/
            
            Vector<Object> resp =  this.dataAccess.access(this.message.info);
            
            if(resp != null){
                info.add("OK");
                Random num = new Random();
                int sendPort = num.nextInt(this.maxPort) + this.minPort;
                i = new Integer(sendPort);
                while(this.usedPorts.contains(i)){
                    sendPort = num.nextInt(this.maxPort) + this.minPort;
                    i = new Integer(sendPort);
                }
                this.usedPorts.add(i);
                info.add(i.toString());
                
                int receivePort = num.nextInt(this.maxPort) + this.minPort;
                i = new Integer(receivePort);
                while(this.usedPorts.contains(i)){
                    receivePort = num.nextInt(this.maxPort) + this.minPort;
                    i = new Integer(receivePort);
                }
                this.usedPorts.add(i);
                info.add(i.toString());
                
                Thread t = new Thread(new DBServer(receivePort, sendPort, dataAccess, this));
                t.start();
            }
            
            else{
                info.add("Invalid Login");
            }
            
            message = new Message(info, false);
            this.enviarMensagem(this.ip, message);
            
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void observe(Vector<Object> string) {
        this.usedPorts.removeElement(new Integer((String) string.elementAt(0)));
        this.usedPorts.removeElement(new Integer((String) string.elementAt(1)));
        Message m = new Message(null, true);
        try {
            this.enviarMensagem(this.ip, m);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void run() {
        System.out.println("start MainServer");
        while(!this.connectionEnded){
            this.conectar();
        }
        System.out.println("end MainServer");
    }
    
    public static void main(String[] args) {
        try {
            MainServer server = new MainServer();
            Thread t = new Thread(server);
            t.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }    
}
